package com.shawn.db.dao;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import com.shawn.db.WatchDogOpenHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by shawn on 2016/4/21.
 */
public class WatchDogDao {

    private WatchDogOpenHelper watchDogOpenHelper;
    private Context context;

    //在构造函数中获取BlackNumOpenHlper
    public WatchDogDao(Context context){
        watchDogOpenHelper = new WatchDogOpenHelper(context);
        this.context = context;
    }

    /**
     * 添加应用程序包名,密码
     * @param packageName
     */
    public void addLockApp(String packageName,String password){
        SQLiteDatabase database = watchDogOpenHelper.getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("packagename", packageName);
        values.put("password",password);
        database.insert(WatchDogOpenHelper.DB_NAME, null, values);

        //通知内容观察者数据库变化了
        ContentResolver resolver = context.getContentResolver();
        //因为是我们自己的数据发生变化了,所以我们要自定义一个uri进行操作
        Uri uri = Uri.parse("content://com.shawn.mobliesafe75.lock.changed");
        resolver.notifyChange(uri,null);

        database.close();
    }

    /**
     * 查询数据库中是否有包名
     * @param packagname
     * @return
     */
    public boolean queryLockApp(String packagname){
        boolean isLock = false;
        SQLiteDatabase database = watchDogOpenHelper.getReadableDatabase();
        Cursor cursor = database.query(WatchDogOpenHelper.DB_NAME, null, "packagename=?", new String[]{packagname}, null, null, null);
        if(cursor.moveToNext()) {
            isLock = true;
        }
        cursor.close();
        database.close();
        return isLock;
    }

    /**
     *  删除包名
     * @param packagename
     */
    public void deleteLockApp(String packagename){
        SQLiteDatabase database = watchDogOpenHelper.getReadableDatabase();
        database.delete(WatchDogOpenHelper.DB_NAME, "packagename=?", new String[]{packagename});
        //通知内容观察者数据库变化了
        ContentResolver resolver = context.getContentResolver();
        //因为是我们自己的数据发生变化了,所以我们要自定义一个uri进行操作
        Uri uri = Uri.parse("content://com.shawn.mobliesafe75.lock.changed");
        resolver.notifyChange(uri, null);

        database.close();
    }

    public List<String> querAllLockApp(){
        List<String> list = new ArrayList<>();
        SQLiteDatabase database = watchDogOpenHelper.getReadableDatabase();
        Cursor cursor = database.query(WatchDogOpenHelper.DB_NAME, new String[]{"packagename"}, null, null, null, null, null);
        while(cursor.moveToNext()) {
            String packagename = cursor.getString(0);
            list.add(packagename);
        }
        cursor.close();
        database.close();
        return list;
    }

    public String queryLockPwd(String packageName){
        SQLiteDatabase database = watchDogOpenHelper.getReadableDatabase();
        Cursor cursor = database.query(WatchDogOpenHelper.DB_NAME, new String[]{"password"}, "packagename=?", new String[]{packageName}, null, null, null);
        cursor.moveToFirst();
        String pwd = cursor.getString(0);
        cursor.close();
        database.close();
        return pwd;
    }
}
